<template>
    <div>
        <el-input v-model="queryData.clientGroupName" placeholder="请输入要搜索群名称" size="small"></el-input>
        <el-input v-model="queryData.empName" placeholder="请输入群主名称" size="small"></el-input>&nbsp;&nbsp;
        <el-button type="primary" @click="queryDataHandle(1, 10)" size="small">查询</el-button>
        <el-button type="primary" @click="resetQuery" size="small">重置</el-button>
        <div class="interspaceH"></div>
        <!--待分配客户群聊-->
        <el-card class="box-card">
            <div slot="header" class="clearfix" style="display:flex;justify-content: space-between;align-items: center">
                <div>
                    <span style="font-size: 12px">共{{ multipleSelection.length }}个待分配客户</span>
                    <span style="color: #9d9d9d;font-size: 12px">上次同步时间{{ synchronizationTime }}</span>
                </div>
                <div style="float: right; padding: 3px 0">
                    <el-button type="primary" @click="assign()" size="small">分配群聊</el-button>
                    <el-button plain size="small" @click="$emit('currentHandler', 'allocation')">分配记录</el-button>
                    <el-button plain size="small" @click="synchronization">同步</el-button>
                </div>
            </div>
            <div>
                <el-table :data="dataSource" style="width: 100%" @selection-change="rowChange"
                          :header-cell-style="{background:'#F3F4F7',color:'#555'}">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column prop="clientGroupName" label="群昵称" :formatter="formatter"></el-table-column>
                    <el-table-column prop="empName" label="群主" :formatter="formatter"></el-table-column>
                    <el-table-column prop="clientNum" label="群人数" :formatter="formatter"></el-table-column>
                    <el-table-column prop="newApplyCnt" label="发起申请数" :formatter="formatter"></el-table-column>
                    <el-table-column prop="newContactCnt" label="新增客户数" :formatter="formatter"></el-table-column>
                    <el-table-column prop="chatCnt" label="聊天总数" :formatter="formatter"></el-table-column>
                    <el-table-column prop="messageCnt" label="员工发送消息数" :formatter="formatter"></el-table-column>
                    <el-table-column prop="avgReplyTime" label="平均首次回复时长" :formatter="formatter"></el-table-column>
                    <el-table-column prop="negativeFeedbackCnt" label="成员删除或加入黑名单的客户数" :formatter="formatter"></el-table-column>
                    <el-table-column prop="createTime" label="创建时间" :formatter="formatter"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button @click="detailHandle(scope.row)" type="text" size="small">详情</el-button>
                            <el-button type="text" size="small" disabled>聊天记录</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <div class="interspaceH"></div>

                <el-pagination @size-change="queryDataHandle(pagePagination.currentPage,$event)"
                               @current-change="queryDataHandle($event,pagePagination.pageSize)"
                               :current-page="pagePagination.currentPage"
                               :page-sizes="pagePagination.pageSizes" :page-size="pagePagination.pageSize"
                               layout="total, sizes, prev, pager, next"
                               :total="pagePagination.total">
                </el-pagination>
            </div>
        </el-card>

        <!-- 客户详情 -->
        <el-dialog title="客户群详情" :visible.sync="detailVisible" width="30%">
            <el-form ref="form" :model="detailForm" label-width="180px">
                <el-form-item label="群昵称：">
                    <el-input type="text" prop="clientGroupName" size="small" style="width: 60%"></el-input>
                </el-form-item>
                <el-form-item label="群主：">
                    <el-input type="text" prop="owner" size="small" style="width: 60%"></el-input>
                </el-form-item>
                <el-form-item label="群标签：">
                    <el-input type="text" prop="tags" size="small" style="width: 60%"></el-input>
                </el-form-item>
                <el-form-item label="群人数：">
                    <el-input type="text" prop="clientNum" size="small" style="width: 60%"></el-input>
                </el-form-item>
                <el-form-item label="今日入群：">
                    <el-input type="text" prop="addToday" size="small" style="width: 60%"></el-input>
                </el-form-item>
                <el-form-item label="今日退群：">
                    <el-input type="text" prop="outToday" size="small" style="width: 60%"></el-input>
                </el-form-item>
                <el-form-item label="创建时间：">
                    <el-input type="text" prop="createTime" size="small" style="width: 60%"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="detailVisible = false">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 分配客户 -->
        <el-dialog title="分配客户" :visible.sync="assignVisible" width="60%" :close-on-click-modal="false">
            <div class="tip" style="margin: 0 0 20px 0"><p>可将选中的客户转接给其他员工，进行后续服务<br>
                注意：90天内客户只能被转移一次，一个客户最多只能被转移两次</p></div>

            <ListSelect @getData="getData" :listFields="listFields" :dataSource="memberData"/>
            <span slot="footer" class="dialog-footer">
                    <el-button @click="assignVisible = false">取 消</el-button>
                <el-button type="primary" @click="assignVisible = false">确 定</el-button>
                </span>
        </el-dialog>
    </div>
</template>

<script>

import ListSelect from "@/components/list_select/list_select"

const queryData = {
    clientGroupName: '',//客户群名称
    empName: ""//群主名称
}

//多选框 - 开始
//多选框 - 结束

export default {
    name: "disclient",
    components: {
        ListSelect
    },
    data() {
        return {

            currentPanel: 'list',
            tablePage: 0,
            detailVisible: false,
            synchronizationTime: this.$moment(this.$t.data.scrm.asyncUnassignedGroupTime).format("YYYY-MM-DD HH:mm:ss"),
            multipleSelection: [],
            dictionary: {
            },
            fieldType: {
                createTime: "date",
            },
            queryData: {...queryData},
            detailForm: {},
            dataSource: [],
            pagePagination: {
                currentPage: 1,
                pageSizes: [10, 15, 20, 25],
                pageSize: 10,
                total: 0
            },
            assignVisible: false,
            memberData: [],
            listFields: {
                deptName: "所属部门",
                empName: "员工名称",
            },
        }
    },
    mounted() {
        this.queryDataHandle()
    },
    methods: {
        currentHandler(type) {
            this.currentPanel = type
        },
        queryDataHandle(page = 1, row = 10) {
            let that = this;

            let payload = {
                ...queryData,
                status: 1,
                pageNum: page,
                pageSize: row,
                empStatus: '5'
            }
            that.$request("/api/custGroup/selectAll/1_0", payload, "POST").then((data) => {
                that.dataSource = data.rows
                that.pagePagination['total'] = data.total
                that.pagePagination['pageSize'] = data.row
            })

        },
        resetQuery() {
            this.queryData = queryData
            this.$message({
                message: '重置成功',
                type: 'success'
            });
        },
        rowChange(val) {
            this.multipleSelection = val;
        },
        synchronization() {
            this.$request("/api/custGroup/saveGroup/1_0", {}, "POST").then((data) => {
                this.synchronizationTime = this.$moment().format("YYYY-MM-DD HH:mm:ss")
                this.$t.data.scrm.asyncUnassignedGroupTime = this.synchronizationTime
                this.$message.success("同步成功")
                this.queryDataHandle()
            })
        },
        detailHandle(row) {
            this.detailForm = this.$utils.formatterForForm(row, this.fieldType, this.dictionary);
            this.detailVisible = true
        },
        formatter(row, column) {
            let field = column.property;
            return this.$utils.formatterForLable(field, row[field], this.fieldType, this['dictionary'][field]);
        },
        assign() {
            let that = this

            if (!that.multipleSelection.length > 0) {
                that.$message.error("未选择客户")
                return
            }

            that.$request("/api/emp/getAllEmp/1_0", {}, "POST").then((data) => {
                that.memberData = data.rows
                that.assignVisible = true
            })
        },
        getData(data) {
            let that = this

            let chat_id_list = []

            this.multipleSelection.map((item) => {
                chat_id_list.push(item.clientGroupId)
            })

            let payload = {
                requestBody: {
                    new_owner: data.empId,
                    chat_id_list
                }
            }

            that.$request("/api/custGroup/groupChatTransfer/1_0", payload, "POST").then((data) => {
                that.assignVisible = false
                that.queryDataHandle()
                that.$message.success("分配成功")
            })
        }
    }
}
</script>

<style scoped>
.el-tab-pane .el-input {
    width: 150px;
}

.el-form-item__content .el-input {
    width: 100%;
}
</style>
